package com.ruix.app.ui.stratum;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.widget.SpringView;
import com.ruix.app.R;
import com.ruix.app.cache.CacheFactory;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.PostListWithTagMapper;
import com.ruix.app.model.Classes;
import com.ruix.app.model.MessageModel;
import com.ruix.app.model.PostsCard;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.adapter.PostTagsAdaptar;
import com.ruix.app.ui.base.BaseActivity;
import com.ruix.app.util.LogUitl;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/7/13.
 */

public class CircleTagActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    public static Intent newIntent(Context context,String currentTag){
        Intent intent = new Intent(context,CircleTagActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("currentTag",currentTag);
        intent.putExtras(bundle);
        return intent;
    }

    private String currentTag;
    private String currentTagString;
    public Classes currentClasses;
    private String cid;
    private ArrayList posts;
    private PostTagsAdaptar adaptar;
    private ListView listView;
    private SwipeRefreshLayout mRefreshLayout;
    private SpringView springView;
    private int LOAD_COUNT = 20;

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_circle_tag;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        currentTag =  intent.getStringExtra("currentTag");
        if (currentTag != null) {
            currentTagString = currentTag.replace("#","");
        }
        cid =  UserManager.sharedInstance().getCurrentClassID();
        setTitle(currentTag);

        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        mRefreshLayout.setColorSchemeResources(R.color.bg_nav_default
                , R.color.bg_nav_yellow
                , R.color.bg_default
                , R.color.text_color_pink);
        listView = (ListView) findViewById(R.id.lv_classes);
        adaptar = new PostTagsAdaptar(this);
        listView.setAdapter(adaptar);

        springView = (SpringView) findViewById(R.id.spring_view);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setGive(SpringView.Give.BOTTOM);
        springView.setFooter(new DefaultFooter(this));
//        noMessageTip = (TextView) mFragmentView.findViewById(R.id.tv_no_data_tip);
    }

    @Override
    protected void initLocalData() {
        postListContainTag(LOAD_COUNT,0,true);
    }

    @Override
    protected void initListener() {
        mRefreshLayout.setOnRefreshListener(this);
        adaptar.setLisentler(new PostTagsAdaptar.PostTagsAdaptarLisentler(){

            @Override
            public void onItemClick(int position) {
                PostsCard postsCard =  adaptar.getItem(position);
                if (postsCard != null){
                    startActivity(PostDetailActivity.newInstant(CircleTagActivity.this,postsCard));
                }
            }
        });


        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadmore() {
                postListContainTag(LOAD_COUNT, adaptar.getCount(), false);
            }
        });
//        springView.setEnable(false);
        mRefreshLayout.setEnabled(true);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                mRefreshLayout.setEnabled(false);
                springView.setEnable(false);
                if (firstVisibleItem == 0) {
                    View firstVisibleItemView = view.getChildAt(0);
                    if (firstVisibleItemView != null && firstVisibleItemView.getTop() == 0) {
                        mRefreshLayout.setEnabled(true);
                    }
                } else if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                    View lastVisibleItemView = view.getChildAt(view.getChildCount() - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == view.getHeight()) {
                        springView.setEnable(true);
                    }
                }

            }
        });
    }



    @Override
    public void onRefresh() {
        postListContainTag(LOAD_COUNT, 0, false);
    }


    private void postListContainTag(int limit, final int offset, final boolean hasLoading){
        if (currentTagString == null){
            return;
        }
        if (hasLoading) {
            showLoading();
        }
        RequestFactory.postContainTag(cid, currentTagString, limit, offset, new PostListWithTagMapper(), new RXObserver<ArrayList<PostsCard>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<PostsCard> value) {
                if (value != null && value.size() > 0) {
                    if (offset > 0) {
                        adaptar.addAll(value);
                    } else {
                        if (value.size() < 20){
                            springView.setEnable(false);
                        }
                        adaptar.replaceAll(value);
//                        String uid =  UserManager.getCurrentUser().getID();
//                        CacheFactory.cacheMessageList(uid,value);
                    }
//                    hideNoMessageTip();
                } else {
                    if (hasLoading) {
                    } else {
                        showSuccessTip("没有更多通知");
                    }
                }

            }

            @Override
            public void onError(Throwable e) {
                if (hasLoading) {
                    dismissLoading();
                } else {
                    mRefreshLayout.setRefreshing(false);
                    springView.onFinishFreshAndLoad();
                }
                resolveException(e);
            }

            @Override
            public void onComplete() {
                if (hasLoading) {
                    dismissLoading();
                } else {
                    mRefreshLayout.setRefreshing(false);
                    springView.onFinishFreshAndLoad();
                }
            }
        });

    }
}
